package com.mjf.net.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketTCPClient {
    public static void main(String[] args) throws IOException {

        // 连接服务端
        Socket s = new Socket("127.0.0.1",9999);

        OutputStream os = s.getOutputStream();
        InputStream is = s.getInputStream();

        os.write("hello java".getBytes());
        os.flush();
        // 禁用此 socket 的输出流
        s.shutdownOutput();
        // 直接关流会导致 socket 关闭
//        os.close();

        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            System.out.println(new String(data, 0 , len));
        }

        // 关闭 socket
        s.close();
    }
}
